package com.ccnf.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ccnf.core.bean.Response;
import com.ccnf.core.bean.ResponseWithData;
import com.ccnf.core.bean.ResponseWithPage;
import com.ccnf.model.entity.CommentThumbs;
import com.ccnf.model.entity.InvitationComment;
import com.ccnf.model.query.CommentQuery;
import com.ccnf.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "CommentController", description = "邀请函评论相关API")
@RestController
@RequestMapping("/invitations/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 分页查询邀请函评论列表信息
     * @param commentQuery 筛选条件
     * @param pageNum 当前显示第几页，默认1
     * @param pageSize 每页显示多少条数据，默认5
     * @return 带分页信息的邀请函评论列表
     */
    @ApiOperation("分页查询邀请函评论列表信息")
    @GetMapping
    public ResponseWithPage invitations(CommentQuery commentQuery,
                                        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = commentService.findComment(commentQuery, page);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    /**
     * 通过评论ID获取评论的信息
     * @param commentId 评论ID
     */
    @ApiOperation("通过评论ID获取评论的信息")
    @GetMapping("{commentId}/user/{userId}")
    public ResponseWithData get(@PathVariable("commentId")
                                @ApiParam("邀请函ID") Long commentId,
                                @PathVariable("userId") Long userId) {
        return ResponseWithData.success(commentService.findByCommentId(commentId, userId));
    }

    /**
     * 新增邀请函评论信息
     * @param invitationComment 新增的评论信息
     */
    @ApiOperation("新增邀请函评论信息")
    @PostMapping
    public ResponseWithData add(@RequestBody InvitationComment invitationComment) {
        commentService.saveInvitationComment(invitationComment);
        return ResponseWithData.success(commentService.findByCommentId(invitationComment.getCommentId(), invitationComment.getUserId()));
    }

    /**
     * 进行评论的点赞操作
     * @param commentThumbs 点赞评论信息
     */
    @ApiOperation("进行评论的点赞操作")
    @PostMapping("thumbs")
    public Response thumbs(@RequestBody CommentThumbs commentThumbs) {
        commentService.commentThumbs(commentThumbs);
        return Response.success();
    }
}